package com.aletto.general_store.presenters;

import android.content.Context;
import android.view.View;

import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.aletto.general_store.R;
import com.aletto.general_store.data.DataRepository;
import com.aletto.general_store.data.model.Goods;
import com.aletto.general_store.data.model.PageResponse;
import com.aletto.general_store.presenters.core.SimplePageTemplate;
import com.aletto.general_store.presenters.core.SimpleParamsTemplate;
import com.aletto.general_store.ui.GoodsGridItemDecoration;
import com.aletto.general_store.ui.activities.GoodsDetailsActivity;
import com.aletto.general_store.ui.adapters.BrowserHistoryAdapter;
import com.aletto.general_store.utils.XHelper;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.viewholder.BaseViewHolder;
import com.scwang.smart.refresh.layout.SmartRefreshLayout;
import com.sisyphus.spinach.core.INetPageTask;
import com.sisyphus.spinach.core.INetTask;
import com.sisyphus.spinach.core.model.IPageResponse;
import com.sisyphus.spinach.core.model.IResponse;
import com.sisyphus.spinach.network.presenters.NetPageTask;
import com.sisyphus.spinach.network.presenters.NetSingleTask;

import java.util.List;

import io.reactivex.rxjava3.core.Observable;
import me.jessyan.autosize.utils.AutoSizeUtils;

public class HistoryPageTemplate extends SimplePageTemplate<Goods> {

    private final BaseQuickAdapter<Goods, BaseViewHolder> mHistoryAdapter;

    private final SimpleParamsTemplate<Object, String> deleteTemplate = new SimpleParamsTemplate<>() {

        @Override
        public INetTask.View<Object> view() {
            return new INetTask.View<>() {
                @Override
                public void success(Object data) {
                    List<Goods> goodsList = mHistoryAdapter.getData();
                    for (int index = 0; index < goodsList.size(); index++) {
                        if (goodsList.get(index).id.equals(params[0])) {
                            mHistoryAdapter.removeAt(index);
                            break;
                        }
                    }
                }
            };
        }

        @Override
        public INetTask.Presenter<Object, INetTask.View<Object>> presenter() {
            return new NetSingleTask<>(DataRepository.getInstance().clearHistory(params[0]));
        }

    };


    public HistoryPageTemplate(RecyclerView recyclerView, SmartRefreshLayout refreshLayout) {
        super(recyclerView, refreshLayout);

        mHistoryAdapter = new BrowserHistoryAdapter();
        recyclerView.setLayoutManager(new GridLayoutManager(getContext(), 3));
        recyclerView.addItemDecoration(new GoodsGridItemDecoration(AutoSizeUtils.dp2px(getContext(), 4)));
    }

    @Override
    public BaseQuickAdapter<Goods, BaseViewHolder> adapter(RecyclerView recyclerView) {

        // 长按删除
        mHistoryAdapter.setOnItemLongClickListener((adapter, view, position) -> {
            View delete = view.findViewById(R.id.button_delete);
            View mask = view.findViewById(R.id.view_background_mask);

            if (delete.getVisibility() == View.VISIBLE) {
                return true;
            }

            XHelper.setVisibility(View.VISIBLE, delete, mask);

            Goods goods = (Goods) adapter.getItem(position);
            delete.setOnClickListener(v -> deleteTemplate.setParams(goods.id).execute());

            return true;
        });


        // 点击查看
        mHistoryAdapter.setOnItemClickListener((adapter, view, position) -> {
            View delete = view.findViewById(R.id.button_delete);
            if (delete.getVisibility() == View.VISIBLE) {
                View mask = view.findViewById(R.id.view_background_mask);
                XHelper.setVisibility(View.INVISIBLE, delete, mask);
                return;
            }

            Context context = view.getContext();
            Goods goods = (Goods) adapter.getItem(position);
            goods.id = goods.goods_id;
            context.startActivity(GoodsDetailsActivity.newIntent(context, goods));
        });

        return mHistoryAdapter;
    }


    @Override
    public INetPageTask.Presenter<Goods, INetPageTask.View<Goods>> presenter() {
        return new NetPageTask<>() {
            @Override
            public Observable<? extends IResponse<? extends IPageResponse<Goods>>> execute(int page) {
                return DataRepository.getInstance().history(page, PageResponse.SIZE);
            }
        };
    }

}
